package com.blog.controller;

import com.blog.entity.Uuu;
import com.blog.service.UuuService;
import com.blog.security.RoleChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/myhome")
public class MyhomeCon {

    @Autowired
    private UuuService uuuService;

    @Autowired
    private RoleChecker roleChecker;

    @GetMapping("/findAll")
    public ResponseEntity<?> findAll() {
        List<Uuu> uuus = uuuService.getAllUuus();
        log.info("返回的Uuu列表大小: {}", uuus.size()); // 增加数据量日志
        return ResponseEntity.ok().body(uuus); // 显式指定返回体
    }

    @PostMapping("/add")
    public ResponseEntity<?> add(@RequestBody Uuu uuu, @RequestHeader("Authorization") String token) {
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        roleChecker.checkAdminRole(token);
        uuuService.addUuu(uuu);
        return ResponseEntity.ok("添加成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable Long id, @RequestHeader("Authorization") String token) {
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        roleChecker.checkAdminRole(token);
        boolean deleted = uuuService.deleteUuu(id);
        if (deleted) {
            return ResponseEntity.ok("删除成功");
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}